<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="84px">
          <el-form-item label="中文名" prop="zhName">
            <el-input v-model="queryParams.zhName" placeholder="请输入中文名" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="queryParams.address" placeholder="请输入地址" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="联系人" prop="contacts">
            <el-input v-model="queryParams.contacts" placeholder="请输入联系人" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="联系方式" prop="contactUs">
            <el-input v-model="queryParams.contactUs" placeholder="请输入联系方式" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Back" @click="back">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table stripe v-loading="loading" :data="customHistoryList" :max-height="tableHeight">
        <el-table-column label="logo" align="center" prop="logo">
          <template v-slot="scope">
            <el-image
              v-if="scope.row.logoList && scope.row.logoList.length"
              style="height: 100px"
              :src="scope.row.logoList[0]"
              fit="contain"
            ></el-image>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column label="中文名" align="center" prop="zhName" show-overflow-tooltip />
        <el-table-column label="英文名" align="center" prop="enName" show-overflow-tooltip />
        <el-table-column label="地址" align="center" prop="address" show-overflow-tooltip />
        <el-table-column label="联系人" align="center" prop="contacts" />
        <el-table-column label="联系方式" align="center" prop="contactUs" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button
                class="light-blue-btn"
                size="default"
                icon="Document"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['wiki:customHistory:edit']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改客户基本信息历史记录对话框 -->
    <el-dialog v-model="dialog.visible" width="50%" append-to-body :close-on-click-modal="false">
      <div v-loading="dialogLoading">
        <el-descriptions title="客户信息" :column="2">
          <!--        <el-descriptions-item label="logo：">-->
          <!--          <el-image v-if="form.logoList && form.logoList.length" style="height: 100px" :src="form.logoList[0]" fit="contain"></el-image>-->
          <!--        </el-descriptions-item>-->
          <el-descriptions-item label="中文名：">{{ form.zhName }}</el-descriptions-item>
          <el-descriptions-item label="英文名：">{{ form.enName }}</el-descriptions-item>
          <el-descriptions-item label="联系人：">{{ form.contacts }}</el-descriptions-item>
          <el-descriptions-item label="联系方式：">{{ form.contactUs }}</el-descriptions-item>
          <el-descriptions-item label="地址：">{{ form.address }}</el-descriptions-item>
        </el-descriptions>
        <div class="detail-box">
          <div>简介：</div>
          <div v-html="form.details"></div>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { listCustomHistory, getCustomHistory } from '@/api/wiki/customHistory';
import { CustomHistoryVO, CustomHistoryQuery, CustomHistoryForm } from '@/api/wiki/customHistory/types';
import {getImage} from "@/utils";

const route = useRoute();
const router = useRouter();
const id = route.params.id as string;
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const customHistoryList = ref<CustomHistoryVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dialogLoading = ref(false);

const queryFormRef = ref<ElFormInstance>();
const customHistoryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const tableHeight = ref(0)
const initFormData: CustomHistoryForm = {
  id: undefined,
  infoId: undefined,
  logo: undefined,
  zhName: undefined,
  enName: undefined,
  details: undefined,
  address: undefined,
  contacts: undefined,
  contactUs: undefined,
  logoList: []
}
const data = reactive<PageData<CustomHistoryForm, CustomHistoryQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    infoId: undefined,
    zhName: undefined,
    enName: undefined,
    address: undefined,
    contacts: undefined,
    contactUs: undefined
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户基本信息历史记录列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.infoId = id
  const res = await listCustomHistory(queryParams.value);
  customHistoryList.value = res.rows;
  customHistoryList.value.forEach((item: CustomHistoryVO) => {
    if(item.logo) {
      getImage(item.logo).then((res) => {
        item.logoList = res
      })
    }
  })
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  customHistoryFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 详情按钮操作 */
const handleUpdate = (row: CustomHistoryVO) => {
  dialog.visible = true;
  dialogLoading.value = true
  dialog.title = "客户基本信息历史记录详情";
  nextTick(async () => {
    reset();
    const _id = row.id
    const res = await getCustomHistory(_id);
    dialogLoading.value = false;
    Object.assign(form.value, res.data);
    getImage(res.data.logo).then((res) => {
      form.value.logoList = res
    })
  });
}

/** 返回按钮操作*/
const back = () => {
  router.push({ path: '/custom/detail/' + id })
}

// /** 导出按钮操作 */
// const handleExport = () => {
//   proxy?.download('wiki/customHistory/export', {
//     ...queryParams.value
//   }, `customHistory_${new Date().getTime()}.xlsx`)
// }

onMounted(() => {
  getList();
  tableHeight.value = document.documentElement.clientHeight - 400
});
</script>
<style scoped lang="scss">
.detail-box {
  display: flex;

  div:first-child {
    width: 50px;
  }

  div:last-child {
    width: calc(100% - 50px);
    //max-height: 300px;
    overflow: auto;
  }
}
</style>
